<template>
  <div class="userlogin">
        <h2>用户登录</h2>
        <div class="userImgPart">
            <div class="userImg">
                <img src="./images/123.png" alt="">
            </div>
        </div>
        <form action="" class="loginform">
            <label class="label">账号</label>
            <input type="text" placeholder="请输入手机号" :value="userName" @input="updateUserName" /><br>
            <label class="label input-jianju">密码</label>
            <input class="input-jianju" type="password" placeholder="请输入密码" :value="userPwd" @input="updateUserPwd"/>
            <img src="./images/警告.png" alt="" class="warn">
            <button class="login" @click.prevent="login" >登录</button>
            <button class="register" @click.prevent="toRegister">立即注册</button>
        </form>
    </div>
</template>

<script>
import { mapState } from 'vuex';

import {getLogin} from '../../api/index';

export default {
    name:'userLogin',
    computed:{
        ...mapState('UserLogin',['userName','userPwd']),
    },
    methods:{
        async login(){
           const result =  await getLogin({username:this.userName,password:this.userPwd})
            if(!result.success){
                alert('登录失败');
                this.$store.commit('UserLogin/STOREUSERREGISTER',{userName:'',password:''});
            }else{
                sessionStorage.setItem('loginToken',result.result.token);
                sessionStorage.setItem('username',this.userName);
                this.$router.push('/userHome/homePage');
            }
        },
        toRegister(){
            this.$router.push('/userRegister');
        },
        updateUserName(e){
            this.$store.commit('UserLogin/UPDATEUSERNAME',e.target.value);
        },
        updateUserPwd(e){
            this.$store.commit('UserLogin/UPDATEUSERPWD',e.target.value);
        },

    }
}
</script>

<style scoped>
    * {
        margin: 0;
        padding: 0;
        height: 100%;
    }
    
    .userlogin {
        width: 95%;
        height: 90%;
        position: absolute;
        top: 5%;
        left: 2.5%;
    }
    
    div h2 {
        color: slategray;
        display: inline;
    }
    
    .userImgPart {
        position: relative;
        height: 150px;
        width: 100%;
    }
    
    .userImg {
        position: relative;
        top: 37.5px;
        height: 75px;
        width: 75px;
        margin: auto;
        border-radius: 37.5px 37.5px;
        border: solid 1px gainsboro;
        overflow: hidden;
    }
    
    .loginInfo {
        margin: 0;
        height: 500px;
    }
    
    .loginform {
        height: 500px;
        display: block;
        font-size: 20px;
        overflow: hidden;
        position: relative;
    }
    
    .label {
        font-size: 20px;
        font-weight: 400;
        margin-left: 3px;
        position: absolute;
        height: 35px;
        line-height: 35px;
    }
    
    input {
        position: absolute;
        width: 95%;
        height: 35px;
        font-size: 20px;
        background: transparent;
        text-indent: 50px;
        border: solid 2px skyblue;
    }
    
    .login {
        position: absolute;
        width: 100%;
        height: 50px;
        bottom: 55px;
        left: 0;
        text-align: center;
        background-color: darkgray;
        border-radius: 5px;
        border: none;
    }
    
    .register {
        position: absolute;
        width: 100%;
        height: 50px;
        bottom: 0px;
        left: 0;
        text-align: center;
        background-color: skyblue;
        border-radius: 5px;
        border: none;
        color: white;
    }
    
    .input-jianju {
        margin-top: 35px;
    }

    .warn{
        position: absolute;
        width:25px;
        height: 25px;
        right: 5%;
        margin-top: 42px;
    }


</style>